﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlMiM.Common.Model;
using System.Data.SqlClient;
using System.Data;
using SqlMiM.Common.Trace;

namespace SqlMiM.Common.Core
{
    public class DatabasesService
    {

        private SqlCommand selectDatabasesCommand;
        private SqlCommand selectDatabasesCommand2;

        #region Singleton

        private static DatabasesService current;

        public static DatabasesService Current
        {
            get
            {
                if (current == null)
                {
                    current = new DatabasesService();
                }

                return current;
            }
        }

        #endregion

        /// <summary>
        /// Get Databases
        /// </summary>
        public List<Database> GetDatabases(bool includeSystemTables)
        {
            SqlConnection connection = Context.Current.GetConnection();
            SqlDataReader reader = null;
            List<Database> dbs = null;

            try
            {
                SqlCommand cmd = includeSystemTables ? this.SelectDatabasesIncludeSystemTablesCommand : this.SelectDatabasesCommand;

                cmd.Connection = connection;

                connection.Open();

                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    dbs = new List<Database>();

                    while (reader.Read())
                    {
                        var db = Database.FillFromReader(reader);
                        dbs.Add(db);
                    }
                }
                reader.Close();

                connection.Close();

                return dbs;

            }
            catch (SqlException sqlEx)
            {
                Notifier.Current.Notify(sqlEx);
            }
            catch (Exception ex)
            {
                Notifier.Current.Notify(ex);
            }
            finally
            {

                if (connection.State != ConnectionState.Closed)
                    connection.Close();

                if (reader != null)
                {
                    reader.Dispose();
                    reader = null;
                }

                if (connection != null)
                {
                    connection.Dispose();
                    connection = null;
                }
            }
            
            return null;
        }


        internal SqlCommand SelectDatabasesCommand
        {
            get
            {
                if (selectDatabasesCommand == null)
                {
                    selectDatabasesCommand = new SqlCommand();
                    selectDatabasesCommand.CommandType = CommandType.Text;
                    selectDatabasesCommand.CommandText = "SELECT * FROM sys.databases where database_id > 4";
                }

                return selectDatabasesCommand;
            }
        }

        internal SqlCommand SelectDatabasesIncludeSystemTablesCommand
        {
            get
            {
                if (selectDatabasesCommand2 == null)
                {
                    selectDatabasesCommand2 = new SqlCommand();
                    selectDatabasesCommand2.CommandType = CommandType.Text;
                    selectDatabasesCommand2.CommandText = "SELECT * FROM sys.databases";
                }

                return selectDatabasesCommand2;
            }
        }

    }
}
